package com.powernode.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.github.pagehelper.PageInfo;
import com.powernode.common.Constants;
import com.powernode.common.Result;
import com.powernode.domain.User;
import com.powernode.dto.UserDto;
import com.powernode.service.impl.UserService;
import com.powernode.utils.WebMVCUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;

    //分页查询用户数据
    @RequestMapping("list.do")
    @ResponseBody
    public Result list(UserDto userDto) {
        System.out.println(userDto);
        PageInfo<User> pageInfo = userService.queryUserForPage(userDto);
        return new Result(0, "查询成功", pageInfo.getTotal(), pageInfo.getList());
    }


    //添加用户
    @PostMapping("addUser.do")
    @ResponseBody
    public Result addUser(User user) {
        try {
            //设置默认密码
            user.setPassword(Constants.USER_DEFAULT);
            //设置在职
            user.setDeleted(Constants.DELETED_1);
            //设置创建更新世间
            user.setCreateTime(new Date());
            int i = this.userService.insertSelective(user);
            //保存
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    //修改
    @PostMapping("updateUser.do")
    @ResponseBody
    public Result updateUser(User user) {
        try {
//设置更新世间   因为数据数据库里为世间戳单位可以不设置更新时间 数据里更新了中国东8区时间，但表格为格林威治时间
            //  user.setUpdateTime(new Date());
            //保存
            this.userService.updateByPrimaryKeySelective(user);
            return Result.OPT_SUCCESS;
        } catch (Exception e) {

            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }


    //用户离职
    @ResponseBody
    @RequestMapping("quit.do")
    public Result quit(Integer id) {
        //  System.out.println("我是离职------------------离职");
        try {
            User user = new User();
            user.setId(id);
            user.setDeleted(Constants.DELETED_2);//离职状态
            user.setDeletedTime(new Date());
            this.userService.updateByPrimaryKeySelective(user);
            System.out.println(user);
            return Result.OPT_SUCCESS;
        } catch (Exception e) {
            return Result.OPT_ERROR;
        }
    }


    //重置密码
    @ResponseBody
    @RequestMapping("resetPwd.do")
    public Result resetPwd(Integer id) {
        try {
            User user = new User();
            user.setId(id);
            user.setPassword(Constants.USER_DEFAULT);
            this.userService.updateByPrimaryKeySelective(user);
            return Result.OPT_SUCCESS;
        } catch (Exception e) {
            return Result.OPT_ERROR;
        }
    }

    /**
     * 查询所有在职的业务员
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("queryAllAvailableUser.do")
    public Result queryAllvailableUser() {
        List<User> userList = this.userService.queryAllvailableUser();
        return new Result(200, "查询成功", 0L, userList);
    }


    //  验证旧密码
    @ResponseBody
    @RequestMapping("checkOldPwd.do")
    public Result checkOldPwd(String oldPwd) {
        System.out.println(oldPwd + "------------" + "验证记密码");
        //取出sisson中的用户id
        Integer id = WebMVCUtils.getCurrentUser().getId();
        User user = this.userService.selectByPrimaryKey(id);
        //根据用户id查询数据库里的用户数据
        if (user == null) {
            return new Result(-1, "用户不存在");
        }
        if (oldPwd.equals(user.getPassword())) {
            return new Result(200, "密码匹配");
        } else {
            return new Result(-1, "密码不匹配");
        }
    }


    //修改密码
    @ResponseBody
    @RequestMapping("updatePwd.do")
    public Result updatePwd(String newPwd) {
     //   System.out.println("我是修改密码" + "----------" + newPwd);
        try {
            //得到当前用户
            Integer id = WebMVCUtils.getCurrentUser().getId();
            //构造用户对象
            User user = new User();
            user.setId(id);
            user.setPassword(newPwd);
            //动态修改
            int i = this.userService.updateByPrimaryKeySelective(user);
            return Result.OPT_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.OPT_ERROR;
        }
    }
}
